package com.py.credit.common;

import com.alibaba.fastjson.JSON;
import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.redis.bis.JedisBisManage;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 单个用户登录
 *
 * @author Leegern
 * @date 2018年4月16日
 */
public class GatewayUserSingleOnline {

    /**
     * 将用户添加到在线列表   username - > jdToken
     *
     * @param usernameId 用户名+id
     * @param jdToken    令牌
     * @return false 可以账号同时多次登录
     */
    public static synchronized boolean putInCache(String usernameId, String jdToken) {
        boolean result = true;
        Jedis jedis = null;
        try {
            jedis = JedisBisManage.getJedis();
            // 存入多个token
            List<String> tokenList = GatewayUserSingleOnline.getUserTokenList(usernameId);
            if (null != tokenList && tokenList.size() > 0) {
                tokenList.add(jdToken);
                jedis.hset(XypjConstants.CREDIT_USER_SINGLE_ONLINE, usernameId, JSON.toJSONString(tokenList));
            } else {
                List<String> list = new ArrayList<String>();
                list.add(jdToken);
                jedis.hset(XypjConstants.CREDIT_USER_SINGLE_ONLINE, usernameId, JSON.toJSONString(list));
            }
        } catch (Exception e) {
            ExceptionLogger.error(e);
        } finally {
            if (null != jedis) {
                JedisBisManage.returnResource(jedis);
            }
        }
        return result;
    }

    /**
     * 清除在线用户令牌
     *
     * @param usernameId 用户名+id
     */
    public static void clearUserTokens(String usernameId) {
        Jedis jedis = null;

        try {
            jedis = JedisBisManage.getJedis();
            jedis.hdel(XypjConstants.CREDIT_USER_SINGLE_ONLINE, usernameId);
        } catch (Exception e) {
            ExceptionLogger.error(e);
        } finally {
            if (null != jedis) {
                JedisBisManage.returnResource(jedis);
            }
        }
    }

    /**
     * 获取用户所有token
     *
     * @param usernameId 用户名+id
     * @return
     */
    public static synchronized List<String> getUserTokenList(String usernameId) {
        Jedis jedis = null;
        List<String> tokenList = new ArrayList<String>();
        try {
            jedis = JedisBisManage.getJedis();
            // 取出用户的所有token
            String tokenJson = jedis.hget(XypjConstants.CREDIT_USER_SINGLE_ONLINE, usernameId);

            if (null != tokenJson && !"".equals(tokenJson)) {
                tokenList = JSON.parseArray(tokenJson, String.class);
            }
        } catch (Exception e) {
            ExceptionLogger.error(e);
        } finally {
            if (null != jedis) {
                JedisBisManage.returnResource(jedis);
            }
        }
        return tokenList;
    }

    /**
     * 清除当前用户token
     *
     * @param usernameId 用户名+id
     * @param token      令牌
     */
    public static void clearCurrentUserToekn(String usernameId, String token) {
        Jedis jedis = null;

        try {
            jedis = JedisBisManage.getJedis();
            Set<String> set = new HashSet<String>(GatewayUserSingleOnline.getUserTokenList(usernameId));
            set.remove(token);
            jedis.hset(XypjConstants.CREDIT_USER_SINGLE_ONLINE, usernameId, JSON.toJSONString(new ArrayList<String>(set)));
        } catch (Exception e) {
            ExceptionLogger.error(e);
        } finally {
            if (null != jedis) {
                JedisBisManage.returnResource(jedis);
            }
        }
    }
}